package com.caldremch.androidcomponent;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.caldremch.component.listner.NavigationBarListner;
import com.caldremch.component.model.LoadStatus;
import com.caldremch.component.widget.NavigationBar;
import com.caldremch.component.widget.StatusInnerView;


/**
 * @author Caldremch
 *
 * @date 2017/4/8
 *
 * @describe 带有头部和加载状态的基类 activity
 *
 **/
public abstract class BaseActivity extends AppCompatActivity {

    protected Activity mContext;
    //view设置
    private View mLayoutView;
    private View mParentContainer;
    private ViewGroup mContentContainer;
    private MyStatusView mStatusView;
    protected View mView;
    protected NavigationBar mNavigationBar;
    private boolean mIsFirstLoading = true;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.activity_baseloading_with_header);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏

        /**
         * view初始化前的一些操作
         */
        beforeInitView();

        initContentView();

        initStatus();

        initView();

        initData();

        initEvent();
    }


    /**
     * 初始化布局
     */
    private void initContentView() {


        //未支持同一个界面 多个地方的加载状态
        mParentContainer = findViewById(R.id.parentContainer);

        //显示内容布局初始化
        mContentContainer = (ViewGroup) findViewById(R.id.contentContainer);

        if (mLayoutView == null) {
            mLayoutView = getLayoutInflater().inflate(getLayout(), null);
        }

        mContentContainer.removeAllViews();
        mContentContainer.addView(mLayoutView);


        //加载时需要展示头部 这里的父容器不一定是根布局 有可能是跟布局底下的某个布局 || 局部状态展示
        //必须: 无论是跟布局还是某些布局: 需要添加状态view的父布局id一定是parentContainer
        if (mParentContainer == null) {
            throw new RuntimeException("Unset the parentView or the id of parentView is not mParentContainer");
        }

        if (mContentContainer == null) {
            throw new RuntimeException("Unset the the contentView or the id of contentView is not mContentContainer");
        }

        if (mParentContainer instanceof RelativeLayout) {

            RelativeLayout rl_main = (RelativeLayout) mParentContainer;
            mStatusView = new MyStatusView(this);
            mStatusView.setStatus(LoadStatus.NORMAL);
            //点击事件
            mStatusView.setOnStatusViewClick(new StatusInnerView.OnStatusViewClick() {
                @Override
                public void onNetErrorClick(View view) {
                    onFailStatusRetry();
                }
            });
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            rl_main.addView(mStatusView, layoutParams);

        } else {

            throw new RuntimeException("the parentView must be RelativeLayout");
        }


        //初始化头部信息
        mNavigationBar = (NavigationBar) findViewById(R.id.navigationBar);

        mNavigationBar.setListner(new NavigationBarListner() {

            @Override
            public void onLeftClick(View v) {
                leftClick();
            }

            @Override
            public void onRightClick(View v) {
                rightClick(v);
            }
        });


    }

    //顶部右边
    protected void rightClick(View v) {

    }

    //顶部左边
    protected void leftClick() {
        finish();
    }

    //失败后重新加载 需要调super().onFailStatusRetry
    protected void onFailStatusRetry() {
        onLoad();
    }

    /**
     * 需要在initView前初始化一些数据, 则重写
     */
    protected void beforeInitView() {

    }

    /**
     * 获取布局
     *
     * @return 布局id
     */
    protected abstract int getLayout();


    /**
     * 初始view ===> findViewById 注解等
     */
    protected abstract void initView();

    /**
     * 设置页面所需的数据
     */
    protected abstract void initData();

    /**
     * 事件监听
     */
    protected abstract void initEvent();


    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);//友盟统计
    }

    @Override
    protected void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);//友盟统计
    }

    //初始化状态
    private void initStatus() {
        if (isNeedLoad()) {
            onLoad();
        }
    }

    //是否需要加载
    protected boolean isNeedLoad() {
        return false;
    }

    //加载状态
    final protected void onLoad() {
        mIsFirstLoading = true;
        setViewVisible(mContentContainer, View.GONE);
        mStatusView.setStatus(LoadStatus.LOADING);
    }

    //加载结束
    final protected void onLoadSucc() {
        mIsFirstLoading = false;
        setViewVisible(mContentContainer, View.VISIBLE);
        mStatusView.setStatus(LoadStatus.NORMAL);

    }

    //加载出错
    final public void onLoadErr() {
        if (mIsFirstLoading) {
            setViewVisible(mContentContainer, View.GONE);
            mStatusView.setStatus(LoadStatus.ERROR);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    /*设置view的隐藏和显示*/
    public static void setViewVisible(View view, int visible) {

        if (view == null) {
            return;
        }
        if (view.getVisibility() != visible) {
            view.setVisibility(visible);
        }
    }
}
